// Plugin dropdown menu
// vnxcoder@gmail.com
(function($)
{
    var VNXDropdown = function(element, options)
    {
        this.$element = $(element);
        this.options = $.extend({}, $.fn.vnxdropdown.defaults, typeof options == 'object' && options);
        var o = this;
        // Add down arrow
        this.$element.children('li:has(ul)').each(function()
        {
            var $this = $(this);
            $('<span>').html('&darr;').css({
                position: 'absolute',
                right: '5px',
                bottom: '5px',
                color: '#fff'
            }).appendTo($this);

            $this.hover(
                function()
                {
                    var $target = $this.children('ul');
                    if ($target.queue().length <= 1) {
                        $target.slideDown(o.options.over);
                    }
                },
                function()
                {
                    var $target = $this.children('ul');
                    $target.slideUp(o.options.out);
                }
            );
        });

        this.$element.find('li ul li:has(ul)').each(function() {
            var $this = $(this);
            $('<span>').html('&rarr;').css({
                position: 'absolute',
                right: '5px',
                bottom: '5px',
                color: '#fff'
            }).appendTo($this);
            $this.hover(
                function()
                {
                    var $target = $this.children('ul');
                    $target.css({
                        left: $this.css('width'),
                        top: 0
                    });
                    if ($target.queue().length <= 1) {
                        $target.slideDown(o.options.over);
                    }
                },
                function()
                {
                    var $target = $this.children('ul');
                    $target.slideUp(o.options.out);
                }
            );
        })
    };

    $.fn.vnxdropdown = function(option)
    {
        return this.each(function()
        {
            var $this = $(this),
                data = $this.data('vnxdropdown'),
                options = $.extend({}, $.fn.vnxdropdown.defaults, $this.data(), typeof option == 'object' && option);
            if (!data) {
                $this.data('vnxdropdown', (data = new VNXDropdown(this, options)));
            }
        });
    };

    $.fn.vnxdropdown.defaults = {
        over: 800,
        out: 600,
        rightImage: 'img/right.gif',
        downImage: 'img/down.gif'
    };
})(jQuery);